package main.controller;

import com.jfoenix.controls.JFXToggleButton;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import main.model.User;
import main.view.Main;
import main.dao.LoginDB;
import java.io.IOException;
import java.sql.SQLException;

public class Login {
    private String inputUsername;
    private String inputPassword;
    public static User momentUser;

    @FXML
    private Button register;

    @FXML
    private Button login;

    @FXML
    private JFXToggleButton isRoot;

    @FXML
    private TextField username;

    @FXML
    private PasswordField password;

    @FXML
    private Text actiontarget;

    @FXML
    void inputPassword(KeyEvent event) {
        this.inputPassword = this.password.getText();
        System.out.println("密码"+this.inputPassword);
    }

    @FXML
    void inputUserName(KeyEvent event) {
        this.inputUsername = this.username.getText();
        System.out.println("学号"+this.inputUsername);
    }

    @FXML
    void judgeUserType(ActionEvent event) throws IOException {
        new Main().initRootLogin();
    }

    @FXML
    void userLogin(ActionEvent event) throws IOException, SQLException {
        if(this.inputUsername==null || this.inputPassword==null){
            actiontarget.setFill(Color.FIREBRICK);
            actiontarget.setText("请输入正确的账户和密码！");
        }else{
            actiontarget.setFill(Color.color(0,0,0));
            actiontarget.setText("login……");
            String str = new LoginDB().login(this.inputUsername,this.inputPassword);
            System.out.println(str);

            if(str.equals("isOK")){
                System.out.println(momentUser.getName());
                new Main().initUserMenu();
            }else{
                actiontarget.setFill(Color.FIREBRICK);
                actiontarget.setText("请输入正确的账户和密码！");
            }

        }
    }

    @FXML
    void userRegister(ActionEvent event) throws IOException {
        new Main().initRegister();
    }
}
